{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 11.1 \u4f5c\u4e3a\u5ba2\u6237\u7aef\u4e0eHTTP\u670d\u52a1\u4ea4\u4e92\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u9700\u8981\u901a\u8fc7HTTP\u534f\u8bae\u4ee5\u5ba2\u6237\u7aef\u7684\u65b9\u5f0f\u8bbf\u95ee\u591a\u79cd\u670d\u52a1\u3002\u4f8b\u5982\uff0c\u4e0b\u8f7d\u6570\u636e\u6216\u8005\u4e0e\u57fa\u4e8eREST\u7684API\u8fdb\u884c\u4ea4\u4e92\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u7b80\u5355\u7684\u4e8b\u60c5\u6765\u8bf4\uff0c\u901a\u5e38\u4f7f\u7528 urllib.request \u6a21\u5757\u5c31\u591f\u4e86\u3002\u4f8b\u5982\uff0c\u53d1\u9001\u4e00\u4e2a\u7b80\u5355\u7684HTTP GET\u8bf7\u6c42\u5230\u8fdc\u7a0b\u7684\u670d\u52a1\u4e0a\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from urllib import request, parse\n\n# Base URL being accessed\nurl = 'http://httpbin.org/get'\n\n# Dictionary of query parameters (if any)\nparms = {\n   'name1' : 'value1',\n   'name2' : 'value2'\n}\n\n# Encode the query string\nquerystring = parse.urlencode(parms)\n\n# Make a GET request and read the response\nu = request.urlopen(url+'?' + querystring)\nresp = u.read()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u9700\u8981\u4f7f\u7528POST\u65b9\u6cd5\u5728\u8bf7\u6c42\u4e3b\u4f53\u4e2d\u53d1\u9001\u67e5\u8be2\u53c2\u6570\uff0c\u53ef\u4ee5\u5c06\u53c2\u6570\u7f16\u7801\u540e\u4f5c\u4e3a\u53ef\u9009\u53c2\u6570\u63d0\u4f9b\u7ed9 urlopen() \u51fd\u6570\uff0c\u5c31\u50cf\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from urllib import request, parse\n\n# Base URL being accessed\nurl = 'http://httpbin.org/post'\n\n# Dictionary of query parameters (if any)\nparms = {\n   'name1' : 'value1',\n   'name2' : 'value2'\n}\n\n# Encode the query string\nquerystring = parse.urlencode(parms)\n\n# Make a POST request and read the response\nu = request.urlopen(url, querystring.encode('ascii'))\nresp = u.read()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u9700\u8981\u5728\u53d1\u51fa\u7684\u8bf7\u6c42\u4e2d\u63d0\u4f9b\u4e00\u4e9b\u81ea\u5b9a\u4e49\u7684HTTP\u5934\uff0c\u4f8b\u5982\u4fee\u6539 user-agent \u5b57\u6bb5,\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5b57\u6bb5\u503c\u7684\u5b57\u5178\uff0c\u5e76\u521b\u5efa\u4e00\u4e2aRequest\u5b9e\u4f8b\u7136\u540e\u5c06\u5176\u4f20\u7ed9 urlopen() \uff0c\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from urllib import request, parse\n...\n\n# Extra headers\nheaders = {\n    'User-agent' : 'none/ofyourbusiness',\n    'Spam' : 'Eggs'\n}\n\nreq = request.Request(url, querystring.encode('ascii'), headers=headers)\n\n# Make a request and read the response\nu = request.urlopen(req)\nresp = u.read()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u9700\u8981\u4ea4\u4e92\u7684\u670d\u52a1\u6bd4\u4e0a\u9762\u7684\u4f8b\u5b50\u90fd\u8981\u590d\u6742\uff0c\u4e5f\u8bb8\u5e94\u8be5\u53bb\u770b\u770b requests \u5e93\uff08https://pypi.python.org/pypi/requests\uff09\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u8fd9\u4e2a\u793a\u4f8b\u91c7\u7528requests\u5e93\u91cd\u65b0\u5b9e\u73b0\u4e86\u4e0a\u9762\u7684\u64cd\u4f5c\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import requests\n\n# Base URL being accessed\nurl = 'http://httpbin.org/post'\n\n# Dictionary of query parameters (if any)\nparms = {\n   'name1' : 'value1',\n   'name2' : 'value2'\n}\n\n# Extra headers\nheaders = {\n    'User-agent' : 'none/ofyourbusiness',\n    'Spam' : 'Eggs'\n}\n\nresp = requests.post(url, data=parms, headers=headers)\n\n# Decoded text returned by the request\ntext = resp.text"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5173\u4e8erequests\u5e93\uff0c\u4e00\u4e2a\u503c\u5f97\u4e00\u63d0\u7684\u7279\u6027\u5c31\u662f\u5b83\u80fd\u4ee5\u591a\u79cd\u65b9\u5f0f\u4ece\u8bf7\u6c42\u4e2d\u8fd4\u56de\u54cd\u5e94\u7ed3\u679c\u7684\u5185\u5bb9\u3002\u4ece\u4e0a\u9762\u7684\u4ee3\u7801\u6765\u770b\uff0c resp.text \u5e26\u7ed9\u6211\u4eec\u7684\u662f\u4ee5Unicode\u89e3\u7801\u7684\u54cd\u5e94\u6587\u672c\u3002\u4f46\u662f\uff0c\u5982\u679c\u53bb\u8bbf\u95ee resp.content \uff0c\u5c31\u4f1a\u5f97\u5230\u539f\u59cb\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u5982\u679c\u8bbf\u95ee resp.json \uff0c\u90a3\u4e48\u5c31\u4f1a\u5f97\u5230JSON\u683c\u5f0f\u7684\u54cd\u5e94\u5185\u5bb9\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u8fd9\u4e2a\u793a\u4f8b\u5229\u7528 requests \u5e93\u53d1\u8d77\u4e00\u4e2aHEAD\u8bf7\u6c42\uff0c\u5e76\u4ece\u54cd\u5e94\u4e2d\u63d0\u53d6\u51fa\u4e00\u4e9bHTTP\u5934\u6570\u636e\u7684\u5b57\u6bb5\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import requests\n\nresp = requests.head('http://www.python.org/index.html')\n\nstatus = resp.status_code\nlast_modified = resp.headers['last-modified']\ncontent_type = resp.headers['content-type']\ncontent_length = resp.headers['content-length']"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u662f\u4e00\u4e2a\u5229\u7528requests\u901a\u8fc7\u57fa\u672c\u8ba4\u8bc1\u767b\u5f55Pypi\u7684\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import requests\n\nresp = requests.get('http://pypi.python.org/pypi?:action=login',\n                    auth=('user','password'))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u662f\u4e00\u4e2a\u5229\u7528requests\u5c06HTTP cookies\u4ece\u4e00\u4e2a\u8bf7\u6c42\u4f20\u9012\u5230\u53e6\u4e00\u4e2a\u7684\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import requests\n\n# First request\nresp1 = requests.get(url)\n...\n\n# Second requests with cookies received on first requests\nresp2 = requests.get(url, cookies=resp1.cookies)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u4f46\u5e76\u975e\u6700\u4e0d\u91cd\u8981\u7684\u4e00\u4e2a\u4f8b\u5b50\u662f\u7528requests\u4e0a\u4f20\u5185\u5bb9\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import requests\nurl = 'http://httpbin.org/post'\nfiles = { 'file': ('data.csv', open('data.csv', 'rb')) }\n\nr = requests.post(url, files=files)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u771f\u7684\u5f88\u7b80\u5355HTTP\u5ba2\u6237\u7aef\u4ee3\u7801\uff0c\u7528\u5185\u7f6e\u7684 urllib \u6a21\u5757\u901a\u5e38\u5c31\u8db3\u591f\u4e86\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u8981\u505a\u7684\u4e0d\u4ec5\u4ec5\u53ea\u662f\u7b80\u5355\u7684GET\u6216POST\u8bf7\u6c42\uff0c\u90a3\u5c31\u771f\u7684\u4e0d\u80fd\u518d\u4f9d\u8d56\u5b83\u7684\u529f\u80fd\u4e86\u3002\u8fd9\u65f6\u5019\u5c31\u662f\u7b2c\u4e09\u65b9\u6a21\u5757\u6bd4\u5982 requests \u5927\u663e\u8eab\u624b\u7684\u65f6\u5019\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u51b3\u5b9a\u575a\u6301\u4f7f\u7528\u6807\u51c6\u7684\u7a0b\u5e8f\u5e93\u800c\u4e0d\u8003\u8651\u50cf requests \u8fd9\u6837\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u90a3\u4e48\u4e5f\u8bb8\u5c31\u4e0d\u5f97\u4e0d\u4f7f\u7528\u5e95\u5c42\u7684 http.client \u6a21\u5757\u6765\u5b9e\u73b0\u81ea\u5df1\u7684\u4ee3\u7801\u3002\u6bd4\u65b9\u8bf4\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u6267\u884c\u4e00\u4e2aHEAD\u8bf7\u6c42\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from http.client import HTTPConnection\nfrom urllib import parse\n\nc = HTTPConnection('www.python.org', 80)\nc.request('HEAD', '/index.html')\nresp = c.getresponse()\n\nprint('Status', resp.status)\nfor name, value in resp.getheaders():\n    print(name, value)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u540c\u6837\u5730\uff0c\u5982\u679c\u5fc5\u987b\u7f16\u5199\u6d89\u53ca\u4ee3\u7406\u3001\u8ba4\u8bc1\u3001cookies\u4ee5\u53ca\u5176\u4ed6\u4e00\u4e9b\u7ec6\u8282\u65b9\u9762\u7684\u4ee3\u7801\uff0c\u90a3\u4e48\u4f7f\u7528 urllib \u5c31\u663e\u5f97\u7279\u522b\u522b\u626d\u548c\u5570\u55e6\u3002\u6bd4\u65b9\u8bf4\uff0c\u4e0b\u9762\u8fd9\u4e2a\u793a\u4f8b\u5b9e\u73b0\u5728Python\u5305\u7d22\u5f15\u4e0a\u7684\u8ba4\u8bc1\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import urllib.request\n\nauth = urllib.request.HTTPBasicAuthHandler()\nauth.add_password('pypi','http://pypi.python.org','username','password')\nopener = urllib.request.build_opener(auth)\n\nr = urllib.request.Request('http://pypi.python.org/pypi?:action=login')\nu = opener.open(r)\nresp = u.read()\n\n# From here. You can access more pages using opener\n..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5766\u767d\u8bf4\uff0c\u6240\u6709\u7684\u8fd9\u4e9b\u64cd\u4f5c\u5728 requests \u5e93\u4e2d\u90fd\u53d8\u5f97\u7b80\u5355\u7684\u591a\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u6d4b\u8bd5HTTP\u5ba2\u6237\u7aef\u4ee3\u7801\u5e38\u5e38\u662f\u5f88\u4ee4\u4eba\u6cae\u4e27\u7684\uff0c\u56e0\u4e3a\u6240\u6709\u68d8\u624b\u7684\u7ec6\u8282\u95ee\u9898\u90fd\u9700\u8981\u8003\u8651\uff08\u4f8b\u5982cookies\u3001\u8ba4\u8bc1\u3001HTTP\u5934\u3001\u7f16\u7801\u65b9\u5f0f\u7b49\uff09\u3002\u8981\u5b8c\u6210\u8fd9\u4e9b\u4efb\u52a1\uff0c\u8003\u8651\u4f7f\u7528httpbin\u670d\u52a1\uff08http://httpbin.org\uff09\u3002\u8fd9\u4e2a\u7ad9\u70b9\u4f1a\u63a5\u6536\u53d1\u51fa\u7684\u8bf7\u6c42\uff0c\u7136\u540e\u4ee5JSON\u7684\u5f62\u5f0f\u5c06\u76f8\u5e94\u4fe1\u606f\u56de\u4f20\u56de\u6765\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u7684\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import requests\nr = requests.get('http://httpbin.org/get?name=Dave&n=37',\n    headers = { 'User-agent': 'goaway/1.0' })\nresp = r.json()\nresp['headers']"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "resp['args']"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8981\u540c\u4e00\u4e2a\u771f\u6b63\u7684\u7ad9\u70b9\u8fdb\u884c\u4ea4\u4e92\u524d\uff0c\u5148\u5728 httpbin.org \u8fd9\u6837\u7684\u7f51\u7ad9\u4e0a\u505a\u5b9e\u9a8c\u5e38\u5e38\u662f\u53ef\u53d6\u7684\u529e\u6cd5\u3002\u5c24\u5176\u662f\u5f53\u6211\u4eec\u9762\u5bf93\u6b21\u767b\u5f55\u5931\u8d25\u5c31\u4f1a\u5173\u95ed\u8d26\u6237\u8fd9\u6837\u7684\u98ce\u9669\u65f6\u5c24\u4e3a\u6709\u7528\uff08\u4e0d\u8981\u5c1d\u8bd5\u81ea\u5df1\u7f16\u5199HTTP\u8ba4\u8bc1\u5ba2\u6237\u7aef\u6765\u767b\u5f55\u4f60\u7684\u94f6\u884c\u8d26\u6237\uff09\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c3d\u7ba1\u672c\u8282\u6ca1\u6709\u6d89\u53ca\uff0c request \u5e93\u8fd8\u5bf9\u8bb8\u591a\u9ad8\u7ea7\u7684HTTP\u5ba2\u6237\u7aef\u534f\u8bae\u63d0\u4f9b\u4e86\u652f\u6301\uff0c\u6bd4\u5982OAuth\u3002 requests \u6a21\u5757\u7684\u6587\u6863\uff08http://docs.python-requests.org)\u8d28\u91cf\u5f88\u9ad8\uff08\u5766\u767d\u8bf4\u6bd4\u5728\u8fd9\u77ed\u77ed\u7684\u4e00\u8282\u7684\u7bc7\u5e45\u4e2d\u6240\u63d0\u4f9b\u7684\u4efb\u4f55\u4fe1\u606f\u90fd\u597d\uff09\uff0c\u53ef\u4ee5\u53c2\u8003\u6587\u6863\u4ee5\u83b7\u5f97\u66f4\u591a\u5730\u4fe1\u606f\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}